function enableCreateNew(){
	$("#divCreateNewGameRecord").fadeIn();
	$("#buttonEnterNewRecord").fadeOut();
}

function cancelNewGameRecord(){
	$("#buttonEnterNewRecord").fadeIn();
	$("#divCreateNewGameRecord").fadeOut();
	$("#divCreateNewGameRecord input").val("");
	clearSubmitResult();
}

function submitNewGameRecord(i_ViewedGameID) {
	$(".inputError").attr('hidden', true);
	clearSubmitResult();
	if (validateNewGameRecord()) {
		sendToServer(i_ViewedGameID);
	}
}

function clearSubmitResult(){
	$("#submitResult").html("");
}

function sendToServer(i_ViewedGameID){
	$.ajax({
		url : "servletGameRecord",
		type : "POST",
		data : {
			action : "insert",
			gameID : i_ViewedGameID,
			playerID : $("#recordPlayer").val(),
			numOfRebuys : $("#recordRebuys").val(),
			winnings : $("#recordWinnings").val(),
			position : $("#recordPosition").val(),
			knockouts: $("#recordKnockouts").val()
		},
		success : function(data,status){
			if (data.report == "success"){
				$("#submitResult").html("Saved record to database!");
				displayGame(i_ViewedGameID);
			}
			else {
				$("#submitResult").html("Something went wrong! Couldn't save new record. :(");
			}
		}
	});
}

function validateNewGameRecord(){
	var isValid = true;
	if ($("#recordRebuy").val() == "") {
		isValid = false;
		$("#RebuysError").attr('hidden', false);
	}
	
	if ($("#recordWinnings").val() == "") {
		isValid = false;
		$("#WinningsError").attr('hidden', false);
	}
	
	if ($("#recordPosition").val() == "") {
		isValid = false;
		$("#PositionError").attr('hidden', false);
	}
	
	if ($("#recordKnockouts").val() == "") {
		isValid = false;
		$("#KnockoutsError").attr('hidden', false);
	}
	return isValid;
}

function displayGame(i_GameID){
	$.ajax({
		url : "ShowSingleGameRecords.jsp",
		type : "POST",
		data : {
			gameID : i_GameID
		},
		success : function(data, status) {
			changeDivMainContainer(data);
		}
	});
}